if CLIENT then return end

if sql.TableExists("TDM_XP_SYSTEM") then
	else
		if (!sql.TableExists("TDM_XP_SYSTEM")) then
			local Query = "CREATE TABLE TDM_XP_SYSTEM ( UniqueID varchar(255), Name varchar(255), Points varchar(255) )"
			local Result = sql.Query(Query)
			if (sql.TableExists("TDM_XP_SYSTEM")) then
			else
			end	
		end
		if (!sql.TableExists("TDM_XP_PLAYER_SYSTEM")) then
			local Query = "CREATE TABLE TDM_XP_PLAYER_SYSTEM ( UniqueID varchar(255), Name varchar(255), Weapon varchar(255), Used varchar(255) )"
			local Result = sql.Query(Query)
			if (sql.TableExists("TDM_XP_PLAYER_SYSTEM")) then
			else
			end	
		end
end

-- Checks to see if the database exists
function TDMXPSYSTEM(ply)
timer.Simple( 2, function()
end)
AddPlayerXPDB(ply)
end
hook.Add( "PlayerInitialSpawn", "TDMXPSYSTEM", TDMXPSYSTEM )


-- Adds Player to the Database
function AddPlayerXPDB(ply)
		local NickName = ply:Nick()
		local PUniqueID = ply:UniqueID()
		local Result = sql.Query( "SELECT UniqueID, Name, Points FROM TDM_XP_SYSTEM WHERE UniqueID = '"..PUniqueID.."'" )
		if (!Result) then
		if ply:SteamID() == "STEAM_0:1:28165373" || ply:SteamID() == "STEAM_0:0:35379784" then
		sql.Query( "INSERT INTO TDM_XP_SYSTEM (`UniqueID`, `Name`, `Points`) VALUES ('"..PUniqueID.."', '"..NickName.."', '999999999')" )
		sql.Query( "INSERT INTO TDM_XP_PLAYER_SYSTEM (`UniqueID`, `Name`, `Weapon`, `Used`) VALUES ('"..PUniqueID.."', '"..NickName.."')" )
		else
		sql.Query( "INSERT INTO TDM_XP_SYSTEM (`UniqueID`, `Name`, `Points`) VALUES ('"..PUniqueID.."', '"..NickName.."', '100')" )
		sql.Query( "INSERT INTO TDM_XP_PLAYER_SYSTEM (`UniqueID`, `Name`, `Weapon`, `Used`) VALUES ('"..PUniqueID.."', '"..NickName.."')" )
		end	
		--	MsgN("[TDM] Player "..NickName.." was added to the database with UniqueID "..PUniqueID.."")
		else
		--	MsgN("[TDM] Player "..NickName.." is already part of the database")
	    sql.Query("UPDATE TDM_XP_SYSTEM SET Name = '"..NickName.."' WHERE UniqueID = '"..PUniqueID.."'")
	    sql.Query("UPDATE TDM_XP_PLAYER_SYSTEM SET Name = '"..NickName.."' WHERE UniqueID = '"..PUniqueID.."'")
		end
end


-- Adds Kill Points to the database
function PlayerDeathGoalTDMXP( ply, wep, killer )
if ply:Team() != killer:Team() || killer == ply then
local OldPoints = sql.QueryValue( "SELECT Points FROM TDM_XP_SYSTEM WHERE UniqueID = '"..killer:UniqueID().."'" )
local NewPoints = OldPoints + 100
sql.Query("UPDATE TDM_XP_SYSTEM SET Points = '"..NewPoints.."' WHERE UniqueID = '"..killer:UniqueID().."'")
end

if ply:Team() == killer:Team() then 
end

end
hook.Add( "PlayerDeath", "PlayerDeathGoalTDMXP", PlayerDeathGoalTDMXP ) 


-- Checks Points
function CheckPlayerPointsXP()
	timer.Create( "CheckXPOnPlayers", 1, 0, function()
	local GetPlayers = player.GetAll()
		for k,v in pairs(GetPlayers) do
			if v:IsValid() then
				local GetPoints = sql.QueryValue( "SELECT Points FROM TDM_XP_SYSTEM WHERE UniqueID = '"..v:UniqueID().."'" )
				umsg.Start("TDMGETPOINTS",v)
				umsg.String(GetPoints)
				umsg.End()
				umsg.Start("TDMGETPOINTS2")
				umsg.String(GetPoints)
				umsg.String(k)
				umsg.End()
			end
		end
	end)
end
timer.Destroy("CheckXPOnPlayers")
timer.Simple( 1, function()
CheckPlayerPointsXP()
end)


-- Buy weapons
function BuyWeaponIndex(ply, cmds, args)
local GetOldPoints = sql.QueryValue( "SELECT Points FROM TDM_XP_SYSTEM WHERE UniqueID = '"..ply:UniqueID().."'" )
GetNewPoints = GetOldPoints - args[2]
if GetNewPoints < 0 then 
umsg.Start("TDMPOINTSARETOOLOW",ply)
umsg.String(GetOldPoints)
umsg.String(args[2])
umsg.End()
return end
sql.Query("UPDATE TDM_XP_SYSTEM SET Points = '"..GetNewPoints.."' WHERE UniqueID = '"..ply:UniqueID().."'")
if ply:Alive() then
if ply:HasWeapon(args[1]) then
ply:StripWeapon(args[1])
ply:Give(args[1])
ply:SelectWeapon(args[1])
umsg.Start("TDMPOINTWEAPON",ply)
umsg.String(args[3])
umsg.String(args[2])
umsg.String(GetNewPoints)
umsg.End()
else
ply:Give(args[1])
ply:SelectWeapon(args[1])
umsg.Start("TDMPOINTWEAPON",ply)
umsg.String(args[3])
umsg.String(args[2])
umsg.String(GetNewPoints)
umsg.End()
end
end
end
concommand.Add( "BuyWeaponIndex", BuyWeaponIndex )


-- Buy other
-- [1]file [2]name [3]points
function BuyOtherIndex(ply, cmds, args)
local GetOldPoints = sql.QueryValue( "SELECT Points FROM TDM_XP_SYSTEM WHERE UniqueID = '"..ply:UniqueID().."'" )
GetNewPoints = GetOldPoints - args[2]
if GetNewPoints < 0 then 
umsg.Start("TDMPOINTSARETOOLOW",ply)
umsg.String(GetOldPoints)
umsg.String(args[2])
umsg.End()
return end
sql.Query("UPDATE TDM_XP_SYSTEM SET Points = '"..GetNewPoints.."' WHERE UniqueID = '"..ply:UniqueID().."'")
umsg.Start("TDMPOINTOTHER")
umsg.String(ply:Nick())
umsg.Short(ply:Team())
umsg.String(args[3])
umsg.String(args[2])
umsg.String(args[1])
umsg.End()
umsg.Start("TDMPOINTOTHER2")
umsg.String(GetNewPoints)
umsg.End()
end
concommand.Add( "BuyOtherIndex", BuyOtherIndex )


function Stop_URL_Song_Client2(ply,text,public)
local exp = string.Explode(" ", text)
if exp[1] == "!stopsong" then
		umsg.Start("stopmysongplease2", ply)
		umsg.End()
end
end

hook.Add("PlayerSay","Stop_URL_Song_Client2", Stop_URL_Song_Client2)


-- Give Money/Points
function TDMGiveMoney(ply, cmds, args)
if args[1] == nil then 
umsg.Start("TDM_Give_Points_ID_Missing",ply)
umsg.End()
return end
if args[2] == nil then 
umsg.Start("TDM_Give_Points_Amount_Missing",ply)
umsg.End()
return end
if args[1] == ply:Nick() then 
umsg.Start("TDM_Give_Points_Self",ply)
umsg.End()
return end
for k, v in pairs(player.GetAll()) do
   if v:Nick() == args[1] then
   GiveMoneyPlayerUniqueID = v:UniqueID()
   GiveMoneyPlayerName = v:Nick()
   GiveMoneyPlayerTeam = v:Team()
   GivePointsSymbol = v
   end
end
GetPOldOldPoints3 = sql.QueryValue( "SELECT Points FROM TDM_XP_SYSTEM WHERE UniqueID = '"..ply:UniqueID().."'" )
local GetOldOldPoints = sql.QueryValue( "SELECT Points FROM TDM_XP_SYSTEM WHERE UniqueID = '"..GiveMoneyPlayerUniqueID.."'" )
GetPOldOldPoints2 = GetPOldOldPoints3 - args[2]
GetOldOldPoints = GetOldOldPoints + args[2]
if tonumber(GetPOldOldPoints2) < 0 then
umsg.Start("TDM_Give_Points_Not_Successfully", ply)
umsg.String(GetPOldOldPoints3)
umsg.String(args[2])
umsg.End()
else
sql.Query("UPDATE TDM_XP_SYSTEM SET Points = '"..GetPOldOldPoints2.."' WHERE UniqueID = '"..ply:UniqueID().."'")
sql.Query("UPDATE TDM_XP_SYSTEM SET Points = '"..GetOldOldPoints.."' WHERE UniqueID = '"..GiveMoneyPlayerUniqueID.."'")
file.Append( "TDM/Logs/"..os.date("%m-%d-%y")..".txt", os.date("%I:%M:%S").." | "..ply:Nick().." has given "..args[2].." points to "..GiveMoneyPlayerName.." \n" )
umsg.Start("TDM_Give_Points_Successfully", ply)
--umsg.String(ply:Nick())
umsg.String(GiveMoneyPlayerName)
umsg.String(args[2])
umsg.Short(ply:Team())
umsg.Short(GiveMoneyPlayerTeam)
umsg.End()
umsg.Start("TDM_PGive_Points_Successfully", GivePointsSymbol)
umsg.String(ply:Nick())
--umsg.String(GiveMoneyPlayerName)
umsg.String(args[2])
umsg.Short(ply:Team())
umsg.Short(GiveMoneyPlayerTeam)
umsg.End()
end
end
concommand.Add( "TDMGiveMoney", TDMGiveMoney )


-- Set Money/Points
function TDMSetMoney(ply, cmds, args)
if !ply:IsSuperAdmin() then
umsg.Start("TDM_Not_Admin",ply)
umsg.End()
else
if args[1] == nil then 
umsg.Start("TDM_Set_Points_ID_Missing",ply)
umsg.End()
return end
if args[2] == nil then 
umsg.Start("TDM_Set_Points_Amount_Missing",ply)
umsg.End()
return end
for k, v in pairs(player.GetAll()) do
   if v:Nick() == args[1] then
   SetMoneyPlayerUniqueID = v:UniqueID()
   SetMoneyPlayerName = v:Nick()
   SetMoneyPlayerTeam = v:Team()
   end
end
sql.Query("UPDATE TDM_XP_SYSTEM SET Points = '"..args[2].."' WHERE UniqueID = '"..SetMoneyPlayerUniqueID.."'")
file.Append( "TDM/Logs/"..os.date("%m-%d-%y")..".txt", os.date("%I:%M:%S").." | "..ply:Nick().." has Set "..args[2].." points to "..SetMoneyPlayerName.." \n" )
umsg.Start("TDM_Set_Points_Successfully")
umsg.String(ply:Nick())
umsg.String(SetMoneyPlayerName)
umsg.String(args[2])
umsg.Short(ply:Team())
umsg.Short(SetMoneyPlayerTeam)
umsg.End()
end
end
concommand.Add( "TDMSetMoney", TDMSetMoney )